/*
 * Home.java
 *
 * Created on January 29, 2007, 11:11 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package ge.info.pages;

import ge.info.domain.City;
import ge.info.domain.Country;
import ge.info.domain.StringString;
import ge.ktapestry.pages.BasePageExt;
import ge.ktapestry.propertyselections.ComboList;
import java.util.ArrayList;
import java.util.List;
import org.apache.tapestry.IRequestCycle;
import org.apache.tapestry.annotations.InitialValue;


public abstract class CityCodes extends BasePageExt
{

    public static int SERVICE_ID = 5;
    
    public static final int COUNTRY = 0;
    public static final int CITY_NAME = 1;
    public static final int CODE = 2;

    public abstract Country getCountry();

    public abstract void setCountry(Country country);

    public abstract List<StringString> getStrings();

    public abstract void setStrings(List<StringString> strings);

    public abstract StringString getString();

    public abstract void setString(StringString string);

    public abstract List<City> getCities();

    public abstract void setCities(List<City> cities);

    public abstract City getCity();

    public abstract void setCity(City city);

//    @Persist
    @InitialValue("literal:")
    public abstract String getCityName();

    public abstract void setCityName(String cityName);

//    @Persist
    @InitialValue("literal:")
    public abstract String getCode();

    public abstract void setCode(String code);

    public abstract boolean isWithCountry();

    public abstract void setWithCountry(boolean withCountry);
    public int submittedFrom = 0;

    public void formSubmit(IRequestCycle cycle)
    {
        if (submittedFrom == COUNTRY)
        {
            // setCountry(null);
            setCityName(null);
            setCode(null);
        } else
        {
            if (submittedFrom == CITY_NAME)
            {
                if (!isWithCountry())
                {
                    setCountry(null);
                }
                setCode(null);
            } else
            {
                if (!isWithCountry())
                {
                    setCountry(null);
                }
                setCityName(null);
            }
        }

    }

    public void submit(IRequestCycle cycle)
    {
        submittedFrom = COUNTRY;
        Country cn = new Country();
        cn.setCountryID(getCountry().getCountryID());
        setStrings(getDbExecutor().searchCountryOperators(cn));
        setCities(getDbExecutor().searchCountryCityCodes(cn));

    }

    public void submitCity(IRequestCycle cycle)
    {
        submittedFrom = CITY_NAME;

        if (getCityName() == null)
        {
            return;
        }
        List<City> cities = getDbExecutor().searchCityCodes(getCityName());
        List<City> cities_cntry = new ArrayList<City>();
        if (isWithCountry())
        {
            Country country = getCountry();
            for (City city : cities)
            {
                if (city.getCountryID().equals(country.getCountryID()))
                {
                    cities_cntry.add(city);
                }
            }

            setCities(cities_cntry);
        } else
        {
            setCities(cities);
        }

    }

    public void submitOperatorCode(IRequestCycle cycle)
    {
        submittedFrom = CODE;
        if (getCode() == null)
        {
            return;
        }
        Country country = new Country();
        country.setCode(getCode());
        if (isWithCountry() && getCountry() != null && getCountry().getCountryID() != -1)
        {
            country.setCountryID(getCountry().getCountryID());
        }

        setStrings(getDbExecutor().searchCountryOperators(country));
    }

    public void submitCityCode(IRequestCycle cycle)
    {
        submittedFrom = CODE;
        if (getCode() == null)
        {
            return;
        }
        List<City> cities = getDbExecutor().searchCityCode(getCode());
        handleWithCountry(cities);

    }

    public void submitCountryCode(IRequestCycle cycle)
    {
        submittedFrom = CODE;
        if (getCode() == null)
        {
            return;
        }
        List<City> cities = getDbExecutor().searchCountryCode(getCode());
        handleWithCountry(cities);
    }

    private void handleWithCountry(List<City> cities)
    {
        List<City> cities_cntry = new ArrayList<City>();
        if (isWithCountry())
        {
            Country country = getCountry();
            for (City city : cities)
            {
                if (city.getCountryID().equals(country.getCountryID()))
                {
                    cities_cntry.add(city);
                }
            }

            setCities(cities_cntry);
        } else
        {
            setCities(cities);
        }

    }

    public String getCountryCode()
    {
        if (getCountry() == null)
        {
            return "";
        }
        return getCountry().getCode();
    }

    public void setCityCountry(IRequestCycle cycle, Integer countryID)
    {
        submittedFrom = COUNTRY;
        List list = ((ComboList) getComboes().getCombo("Countries")).getOptions();
        Country country = null;
        for (Object o : list)
        {
            if (((Country) o).getCountryID().equals(countryID))
            {
                country = (Country) o;
                break;
            }
        }
        setCountry(country);
    }

     public void sendSMSCountry(IRequestCycle cycle, Country country)
    {


        StringBuilder sb = new StringBuilder();
        if (country!=null)
        {
        sb.append(country.getCountryName()).append(" ");
        sb.append(country.getCode()).append(" ");
        }



        getDbExecutor().insertSms(getSession(), sb.toString(), SERVICE_ID);
    }

      public void sendSMSCity(IRequestCycle cycle, City city)
    {


        StringBuilder sb = new StringBuilder();
        if (city!=null)
        {
        //sb.append(country.getCountryName()).append(" ");
           sb.append("qveynis kodi ");
        sb.append(city.getCountryCode()).append(" ");
        sb.append(city.getCityName()).append(" ");
        sb.append(city.getCode()).append(" ");
        }



        getDbExecutor().insertSms(getSession(), sb.toString(), SERVICE_ID);
    }
      public void sendSMSMob(IRequestCycle cycle, StringString string)
    {


        StringBuilder sb = new StringBuilder();
        if (string!=null)
        {
       
        sb.append(string.getStringOne()).append(" ");
        sb.append(string.getStringTwo()).append(" ");
        }



        getDbExecutor().insertSms(getSession(), sb.toString(), SERVICE_ID);
    }
}
